-----
-- Wave Theme
--
-- Author: Mladen Mijatov
-- Contact: meaneye.rcf@gmail.com
-----

require("awful.util")

reverse_order = true
create_widgets = true
create_clock = true
create_volume = false

theme_directory = awful.util.getdir("config") .. "/wave/"

theme = {}
theme.font = "Ubuntu 9"

theme.bar_height = 22

theme.bg_normal = "#222222"
theme.bg_focus = "#535d6c"
theme.bg_urgent = "#ed8c2e"
theme.bg_minimize = "#444444"

theme.fg_normal = "#aaaaaa"
theme.fg_focus = "#ffffff"
theme.fg_urgent = "#ffffff"
theme.fg_minimize = "#ffffff"

theme.border_width = "1"
theme.border_normal = "#222222"
theme.border_focus = "#256fb4"
theme.border_marked = "#91231c"

theme.taglist_bg_focus = "#1b3950"
theme.taglist_fg_focus = "#ffffff"

-- Display the taglist squares
theme.taglist_squares_sel = nil
theme.taglist_squares_unsel = nil

-- theme.tasklist_floating_icon = nil

-- Variables set for theming the menu:
theme.menu_submenu_icon = theme_directory .. "images/submenu.png"
theme.menu_height = "20"
theme.menu_width  = "150"
theme.menu_border_width = "0"
theme.menu_bg_normal = "#1b3950"
theme.menu_bg_focus = "#256fb4"
theme.menu_fg_normal = "#aaaaaa"
theme.menu_fg_focus = "#ffffff"

-- Define the image to load
-- theme.titlebar_close_button_normal = nil
-- theme.titlebar_close_button_focus  = nil

-- theme.titlebar_ontop_button_normal_inactive = nil
-- theme.titlebar_ontop_button_focus_inactive = nil
-- theme.titlebar_ontop_button_normal_active = nil
-- theme.titlebar_ontop_button_focus_active = nil

-- theme.titlebar_sticky_button_normal_inactive = nil
-- theme.titlebar_sticky_button_focus_inactive = nil
-- theme.titlebar_sticky_button_normal_active = nil
-- theme.titlebar_sticky_button_focus_active = nil

-- theme.titlebar_floating_button_normal_inactive = nil
-- theme.titlebar_floating_button_focus_inactive = nil
-- theme.titlebar_floating_button_normal_active = nil
-- theme.titlebar_floating_button_focus_active = nil

-- theme.titlebar_maximized_button_normal_inactive = nil
-- theme.titlebar_maximized_button_focus_inactive = nil
-- theme.titlebar_maximized_button_normal_active = nil
-- theme.titlebar_maximized_button_focus_active = nil

-- You can use your own command to set your wallpaper
theme.wallpaper_cmd = {'nitrogen --restore'}

-- You can use your own layout icons like this:
theme.layout_fairh = "/usr/share/awesome/themes/default/layouts/fairhw.png"
theme.layout_fairv = "/usr/share/awesome/themes/default/layouts/fairvw.png"
theme.layout_floating  = "/usr/share/awesome/themes/default/layouts/floatingw.png"
theme.layout_magnifier = "/usr/share/awesome/themes/default/layouts/magnifierw.png"
theme.layout_max = "/usr/share/awesome/themes/default/layouts/maxw.png"
theme.layout_fullscreen = "/usr/share/awesome/themes/default/layouts/fullscreenw.png"
theme.layout_tilebottom = "/usr/share/awesome/themes/default/layouts/tilebottomw.png"
theme.layout_tileleft   = "/usr/share/awesome/themes/default/layouts/tileleftw.png"
theme.layout_tile = "/usr/share/awesome/themes/default/layouts/tilew.png"
theme.layout_tiletop = "/usr/share/awesome/themes/default/layouts/tiletopw.png"
theme.layout_spiral  = "/usr/share/awesome/themes/default/layouts/spiralw.png"
theme.layout_dwindle = "/usr/share/awesome/themes/default/layouts/dwindlew.png"

theme.awesome_icon = theme_directory .. "images/awesome.png"
theme.widgets = {}

-- Create widget set
if create_widgets then
	require("vicious")

	-- CPU
	cpu_label = widget({type = "textbox"})
	cpu_image = widget({type = "imagebox", align = "middle"})
	cpu_image.image = image(theme_directory .. "images/status/cpu.png")
	vicious.register(cpu_label, vicious.widgets.cpu, "CPU: $1%")

	cpu_label.width = 80
	cpu_image.scale = false

	-- Memory
	memory_label = widget({type = "textbox"})
	memory_image = widget({type = "imagebox"})
	memory_image.image = image(theme_directory .. "images/status/memory.png")
	vicious.register(memory_label, vicious.widgets.mem, "RAM: $2MB ($1%)", 10)

	memory_label.width = 150

	-- Upload and download
	net_label = widget({type = "textbox"})
	download_label = widget({type = "textbox"})
	download_image = widget({type = "imagebox"})
	upload_label = widget({type = "textbox"})
	upload_image = widget({type = "imagebox"})

	download_image.image = image(theme_directory .. "images/status/download.png")
	upload_image.image = image(theme_directory .. "images/status/upload.png")

	net_label.width = 60
	download_label.width = 60
	upload_label.width = 80

	-- Detect network interface
	local interface = "lo"
	local wifi_interface = nil

	for line in io.lines("/proc/net/dev") do
		local name = string.match(line, "^[%s]?[%s]?[%s]?[%s]?([%w]+):")

		if name ~= nil and string.find(name, "wlan") then
			wifi_interface = name
		end

		if name ~= nil and name ~= "lo" then
			interface = name
			break
		end
	end

	net_label.text = "Net: " .. interface

	vicious.register(download_label, vicious.widgets.net, "${" .. interface .. " down_kb}kB")
	vicious.register(upload_label, vicious.widgets.net, "${" .. interface .. " up_kb}kB")

	-- Add wireless if needed
	wifi_label = nil
	wifi_image = nil

	if wifi_interface ~= nil then
		wifi_label = widget({type = "textbox"})
		wifi_image = widget({type = "imagebox"})
		wifi_image.image = image(theme_directory .. "images/status/wireless.png")
		vicious.register(wifi_label, vicious.widgets.wifi, "${sign}dBi ${ssid}", 10, wifi_interface)

		wifi_label.width = 200
	end

	-- Battery
	battery_name = nil
	battery_label = nil
	battery_image = nil

	for item in io.popen("ls /sys/class/power_supply"):lines() do
		if string.find(item, "BAT") then
			battery_name = item
			break
		end
	end

	if battery_name ~= nil then
		battery_label = widget({type = "textbox"})
		battery_image = widget({type = "imagebox"})
		battery_image.image = image(theme_directory .. "images/status/battery.png")
		vicious.register(battery_label, vicious.widgets.bat, "$2% - $3", 60, "BAT0")

		battery_label.width = 100
	end

	-- Volume
	volume_label = nil
	volume_image = nil

	if create_volume then
		volume_label = widget({type = "textbox"})
		volume_image = widget({type = "imagebox"})
		volume_image.image = image(theme_directory .. "images/status/volume.png")
		vicious.register(volume_label, vicious.widgets.volume, "$1%", 10, "Master")

		volume_label.width = 70
	end
	
	-- Clock
	clock_image = nil
	clock_label = nil

	if create_clock then
		clock_label = widget({type = "textbox"})
		clock_image = widget({type = "imagebox"})
		clock_image.image = image(theme_directory .. "images/status/clock.png")
		vicious.register(clock_label, vicious.widgets.date, "%b %d, %R", 60)

		clock_label.width = 100
	end

	-- Pack widgets
	if not reverse_order then
		theme.widgets = {
			cpu_image,
			cpu_label,
			memory_image,
			memory_label,
			net_label,
			download_image,
			download_label,
			upload_image,
			upload_label,
			volume_image,
			volume_label,
			battery_image,
			battery_label,
			wifi_image,
			wifi_label,
			clock_image,
			clock_label,
			layout = awful.widget.layout.horizontal.leftright
		}
	else
		theme.widgets = {
			clock_label,
			clock_image,
			wifi_label,
			wifi_image,
			battery_label,
			battery_image,
			volume_label,
			volume_image,
			upload_label,
			upload_image,
			download_label,
			download_image,
			net_label,
			memory_label,
			memory_image,
			cpu_label,
			cpu_image,
			layout = awful.widget.layout.horizontal.rightleft
		}
	end
end

return theme
